package cn.dapeng.t01;

/**
 * @BelongProject study
 * @BelongPackage cn.dapeng.t01
 * @Description TODO
 * @ClassName ArraySunT01
 * @Author xupenghong
 * @Date 2024/8/27 11:01
 * @Version 1.0.0
 */
public class ArraySunT01 {

    public static int sum0(int[] array) {
        if (array == null || array.length == 0) {
            return 0;
        }
        int sum = array[0];
        for (int i = 1; i < array.length; i ++) {
            sum += getIndex(array, i);
        }
        return sum;
    }

    public static int sum1(int[] array) {
        if (array == null || array.length == 0) {
            return 0;
        }
        int sum = array[0];
        sum += sum1(cutArray(array));
        return sum;
    }

    private static int[] cutArray(int[] array) {
        int length = array.length - 1;
        int[] newArray = new int[length];
        if (length > 0) System.arraycopy(array, 1, newArray, 0, length);
        return newArray;
    }

    private static int getIndex(int[] array, int index) {
        return array[index];
    }

    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 7, 8};
        System.out.println(sum0(array));
        System.out.println();

    }

}
